package com.swpu.cbg.cnshop.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.squareup.okhttp.Response;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.User;
import com.swpu.cbg.cnshop.http.OkHttpHelper;
import com.swpu.cbg.cnshop.http.SpotsCallback;
import com.swpu.cbg.cnshop.msg.LoginResMsg;
import com.swpu.cbg.cnshop.utils.DESUtil;
import com.swpu.cbg.cnshop.utils.ToastUtil;
import com.swpu.cbg.cnshop.utils.UserLocalData;
import com.swpu.cbg.cnshop.widget.ClearEditText;
import com.swpu.cbg.cnshop.widget.cnToolbar;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenboge on 16/2/18.
 */
public class LoginActivity extends BaseActivity {

    private OkHttpHelper mHelper;

    @ViewInject(R.id.toolbar)
    private cnToolbar mToolbar;

    @ViewInject(R.id.etxt_phone)
    private ClearEditText PhoneText;

    @ViewInject(R.id.etxt_pwd)
    private ClearEditText pwdText;

    @ViewInject(R.id.btn_login)
    private Button loginBtn;

    @ViewInject(R.id.txt_toReg)
    private TextView registerText;

    @ViewInject(R.id.txt_find_pwd)
    private TextView findPwdText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ViewUtils.inject(this);
        mHelper = OkHttpHelper.getInstance();
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.hideContent();
        mToolbar.showTitle();
        mToolbar.showRightButton();
    }

    @OnClick(R.id.btn_login)
    private void login(View v) {

        String phone = PhoneText.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast(this, "请输入手机号码");
            return;
        }

        String pwd = pwdText.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }

        Map<String, Object> params = new HashMap<>(2);
        params.put("phone", phone);
        params.put("password", DESUtil.encode(Constans.DES_KEY, pwd));

        mHelper.post(Constans.API.LOGIN, params, new SpotsCallback<LoginResMsg<User>>(this) {

            @Override
            public void onSuccess(Response response, LoginResMsg<User> userLoginResMsg) {
                MyApplication app = MyApplication.getInstanse();
                User user = userLoginResMsg.getData();
               // String img="http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=美女图片&pn=55&spn=0&di=0&pi=9476557800&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=1076614387%2C3811057563&os=&simid=&adpicid=0&ln=31000&fr=&fmq=1378374347070_R&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=girl&cg=girl&bdtype=-1&objurl=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F71cf3bc79f3df8dc39cb6295cf11728b461028c4.jpg&fromurl=http%3A%2F%2Fwww.mn123.net%2Fwangluomeinv%2F3394_3.html&gsm=";
                user.setLogo_url("http://7mno4h.com2.z0.glb.qiniucdn.com/555c6c90Ncb4fe515.jpg");
                user.setUsername("白哥哥");
                app.putUser(user, userLoginResMsg.getToken());
                if (MyApplication.getInstanse().getIntent() == null) {
                    setResult(RESULT_OK);
                    finish();
                } else {
                    MyApplication.getInstanse().jumpToIntent(LoginActivity.this);
                    finish();
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }

            @Override
            public void onTokenError(Response response, int code) {

            }
        });


    }


    @OnClick(R.id.txt_toReg)
    private void register(View v) {
        Intent intent = new Intent();
        intent.setClass(this, RegisterActivity.class);
        startActivityForResult(intent, Constans.State.STATE_OK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constans.State.STATE_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
